package com.mmall.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * Created by Administrator on 2017/11/10.
 */
public class QiniuUtil {
    private static String ACCESS_KEY = "240MvO0h7ij2fgJvT9fr6W04Nkvp44TNZgEkUGeL";
    private static String SECRET_KEY = "-0bnrY6kjkOdPQ_k4WIVD4Zqd0-k2IEbT-2MfJbK";
    private static String bucketname = "yanglei";
    private static Auth auth;
    private static UploadManager uploadManager;

    /*密钥配置*/
    static {
        auth=Auth.create(ACCESS_KEY,SECRET_KEY);
        //自动识别要上传的空间（bucket）的存储区域是华东、华北、华南
        Zone zone = Zone.autoZone();
        Configuration configuration = new Configuration(zone);
        uploadManager = new UploadManager(configuration);
    }

    private static String getUpToken(){
        return auth.uploadToken(bucketname,null,1800,null);
    }

    public static String Upload(String filePath) throws QiniuException {
        Response put = uploadManager.put(filePath, null, getUpToken());     //调用put方法上传
        return put.bodyString();
    }

    public static void main(String[] args) throws QiniuException {
        String filePath="C:\\Users\\Public\\Pictures\\Sample Pictures\\2029410it6ojocj3icjokc.jpg";
        String upload = QiniuUtil.Upload(filePath);
        System.out.println(upload);
    }
}
